package comWorld.social;

import java.util.Date;
import java.util.Calendar;
import java.util.GregorianCalendar;

import com.gjs.tools.Date.DateUtil;
import com.gjs.tools.JDBC.JDBCTools;

public class VisitorCount {
	private int id;
	private int today = 0;
	private String todayDate;
	private Calendar todayCalendar;
	private int yesterday = 0;
	private int week = 0;
	private int month = 0;
	private int allCount = 0;

	public VisitorCount() {
		
	}
	
	public VisitorCount(int id) {
		this.id = id;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getToday() {
		return today;
	}

	public void setToday(int today) {
		this.today = today;
	}

	public int getYesterday() {
		return yesterday;
	}

	public void setYesterday(int yesterday) {
		this.yesterday = yesterday;
	}

	public int getWeek() {
		return week;
	}

	public void setWeek(int week) {
		this.week = week;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public Calendar getTodayCalendar() {
		return todayCalendar;
	}

	public void setTodayCalendar(Calendar todayCalendar) {
		this.todayCalendar = todayCalendar;
	}

	public int getAllCount() {
		return allCount;
	}

	public void setAllCount(int allCount) {
		this.allCount = allCount;
	}

	public void add() {
		Calendar realTodayCalendar = new GregorianCalendar();
		boolean dif = false;
		if (realTodayCalendar.get(Calendar.YEAR) != todayCalendar.get(Calendar.YEAR))
			dif = true;
		if (realTodayCalendar.get(Calendar.MONTH) != todayCalendar.get(Calendar.MONTH))
			dif = true;
		if (realTodayCalendar.get(Calendar.DATE) != todayCalendar.get(Calendar.DATE))
			dif = true;
		if (dif) {
			todayCalendar = realTodayCalendar;
			todayDate = DateUtil.formatDate(todayCalendar.getTime(), DateUtil.DATESTRFORMAT);
			today = 0;
		}
		today++;
		allCount++;
		updateVisitorCount();
	}
	
	public String getTodayDate() {
		return todayDate;
	}

	public void setTodayDate(String todayDate) {
		this.todayDate = todayDate;
		todayCalendar = new GregorianCalendar();
		todayCalendar.setTime(DateUtil.parseDate(todayDate, DateUtil.DATESTRFORMAT));
	}

	public static VisitorCount getLastVisitorCount(int id) {
		String sql = "select id, today, yesterday, week, month, all_count allCount, today_date todayDate from visitor_count where id = ?";
		VisitorCount visitorCount = JDBCTools.getObject(VisitorCount.class, sql, id);
		return visitorCount;
	}
	
	public static int addNewVisitorCount(int id) {
		String sql2 = "insert into visitor_count values(?, 0, 0, 0, 0, 0, ?)";
		return JDBCTools.insert(sql2, id, DateUtil.formatDate(new Date(), DateUtil.DATESTRFORMAT));
	}
	
	public boolean updateVisitorCount() {
		String sql = "UPDATE visitor_count SET today = ?, yesterday = ?, week = ?, month = ?, all_count = ?, today_date = ? WHERE id = ?";
		int count = JDBCTools.update(sql, getToday(), getYesterday(), getWeek(), getMonth(), getAllCount(), getTodayDate(), getId());
		return count > 0;
		
	}
}
